//
//  ButtonViewController.m
//  11105
//
//  Created by student on 16/2/29.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ButtonViewController.h"

@interface ButtonViewController ()<UIScrollViewDelegate>
@property(weak,nonatomic)IBOutlet UIScrollView *scrollview;
@property(weak,nonatomic)IBOutlet UIPageControl *pagecontrol;
@property(weak,nonatomic)IBOutlet UIButton *button;
@end


@implementation ButtonViewController

-(id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if(self)
    {
        UIScrollView *scrollview=[[UIScrollView alloc]init];
        [self addSubview:scrollview];
        scrollview.pagingEnabled=YES;
        scrollview.showsHorizontalScrollIndicator=NO;
        self.scrollview=scrollview;
        self.scrollview.delegate=self;
        
        
        
        UIPageControl *pagecontrol=[[UIPageControl alloc]init];
        pagecontrol.numberOfPages=6;
        pagecontrol.currentPage=0;
        
        pagecontrol.pageIndicatorTintColor=[UIColor redColor];
        pagecontrol.currentPageIndicatorTintColor=[UIColor yellowColor];
        [self addSubview:pagecontrol];
        self.pagecontrol=pagecontrol;
        
        for(int i=0;i<self.btnname.count;i++)
        {
            UIButton *button=[[UIButton alloc]init];
            [button setTitle:self.btnname[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [self addSubview:button];
            self.button=button;
        }
    }
    return self;
}
-(void)layoutSubviews



{
    [super layoutSubviews];
    self.scrollview.frame=self.bounds;
    
    CGFloat btnwidth=self.scrollview.bounds.size.width/4;
    CGFloat btnheight=self.scrollview.bounds.size.width/3;
    for(int i=0;i<self.btnname.count;i++)
    {
        UIButton *button=self.scrollview.subviews[i];
        button.frame=CGRectMake(btnwidth *i+btnwidth/3, 0,btnwidth, btnheight);
    }

    self.scrollview.contentSize=CGSizeMake(btnwidth*8, 0);
        CGFloat pageW = 100;
    CGFloat pageH = 20;
    CGFloat pageX = self.bounds.size.width/2 - pageW/2;
    CGFloat pageY = self.bounds.size.height - pageH;
    self.pagecontrol.frame=CGRectMake(pageX, pageY, pageW, pageH);
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pagecontrol.currentPage=scrollView.contentOffset.x/400;
}
@end
